<template>
  <div class="script-run">
    <el-table border :data="tableData">
      <el-table-column prop="name" :label="$t('script.名称')" align="center" width="200">
      </el-table-column>
      <el-table-column prop="url" :label="$t('script.URL地址')" align="center">
      </el-table-column>
      <el-table-column prop="param" :label="$t('script.参数')" align="center" width="200">
      </el-table-column>
      <el-table-column label="运行" align="center" width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">
            {{ $t("form.runBtn") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.start"
        background
        :page-sizes="[15, 100, 200, 300, 400]"
        :page-size="page.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { scriptListApi, scriptRunApi } from "@/api";
import { cloneDeep } from "loadsh";
export default {
  name: "script-run",
  data() {
    return {
      tableData: [],
      page: {
        start: 1,
        limit: 15,
      },
      total: 0,
    };
  },
  created() {
    this.getScriptList();
  },
  methods: {
    async getScriptList() {
      let params = cloneDeep({
        ...this.page,
        start: this.page.start - 1,
      });
      const { data, total } = await scriptListApi(params);
      this.tableData = data;
      this.total = total;
    },
    async handleClick({ param }) {
      await scriptRunApi({ param });
      this.$notify({
        title: "成功",
        message: "脚本执行成功",
        type: "success",
      });
    },
    handleSizeChange(e) {
      this.page.limit = e;
      this.getScriptList();
    },
    handleCurrentChange(e) {
      this.page.start = e;
      this.getScriptList();
    },
  },
};
</script>

<style lang="scss" scoped src="./script-run.scss"></style>
